#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int parse_string(char original[], char* buffer[])
{
	int fields=0,ctx_i;
	int i,platform_str_l, orig_l, parentheses_ctx=0, browser_inf=0, parentheses_ignore=0;
	printf("%s\n", original);
	orig_l = strlen(original);
	//printf("len: %d\n", orig_l);
	
	for(i = 0 ; i < orig_l; i++)
	{
		if(parentheses_ctx==0 && original[i] == '('){
			parentheses_ctx = 1;
			platform_str_l = 0;
			continue;
		}
		else if(parentheses_ctx==1 && original[i] == ')'){
			parentheses_ctx = 2;
			browser_inf=1;
			continue;
		}
		else if(parentheses_ctx==1)
		{
			buffer[0][platform_str_l] = original[i];
			platform_str_l++;
			continue;
		}

		else if(browser_inf && parentheses_ignore==0 && original[i] == '('){
			parentheses_ignore=1;
			continue;
		}
		else if(browser_inf && parentheses_ignore==1 && original[i] == ')')
		{
			parentheses_ignore=2;
			fields--;
			continue;
		}
		else if(browser_inf && parentheses_ignore == 1)
		{
			continue;
		}

		else if(browser_inf && original[i] == ' ')
		{
			fields++;
			ctx_i=0;
			continue;
		}
		else if(browser_inf){
			buffer[fields][ctx_i] = original[i];
			ctx_i++;
			continue;
		}
		else{
			continue;
		}
	}
	
	return fields+1;
}

#define SUBSTR_L	1024
#define SUBSTR_N	10

void flush_buffer(char* buffer[])
{
	int i;
	for(i = 0; i < SUBSTR_N; i++){ 
		memset(buffer[i], 0, SUBSTR_L); 
	}
}

char *browser_agent_msie="Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
char *browser_agent_iphonesafari="Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1";
char *browser_agent_firefox="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0";
char *browser_agent_msedge="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063";
char *browser_agent_chrome="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";

int main(int argc, char* argv[])
{
	char** buffer = (char**)malloc(sizeof(char*)*SUBSTR_N);
	int i; for(i = 0; i < SUBSTR_N; i++){ 
		buffer[i]=(char*)malloc(sizeof(char)*SUBSTR_L);
		memset(buffer[i], 0, SUBSTR_L); 
	}

	int fld;
	fld = parse_string(browser_agent_msie, buffer);printf("total <%d> fields.\n", fld);for(i = 0; i < SUBSTR_N; i++){ printf("buffer#%d -> %s\n", i, buffer[i]); } flush_buffer(buffer);
	fld = parse_string(browser_agent_iphonesafari, buffer);printf("total <%d> fields.\n", fld);for(i = 0; i < SUBSTR_N; i++){ printf("buffer#%d -> %s\n", i, buffer[i]); } flush_buffer(buffer);
	fld = parse_string(browser_agent_firefox, buffer);printf("total <%d> fields.\n", fld);for(i = 0; i < SUBSTR_N; i++){ printf("buffer#%d -> %s\n", i, buffer[i]); } flush_buffer(buffer);
	fld = parse_string(browser_agent_msedge, buffer);printf("total <%d> fields.\n", fld);for(i = 0; i < SUBSTR_N; i++){ printf("buffer#%d -> %s\n", i, buffer[i]); } flush_buffer(buffer);
	fld = parse_string(browser_agent_chrome, buffer);printf("total <%d> fields.\n", fld);for(i = 0; i < SUBSTR_N; i++){ printf("buffer#%d -> %s\n", i, buffer[i]); } flush_buffer(buffer);

	for(i = 0; i < SUBSTR_N; i++){
		if(buffer[i]!=NULL)
			free(buffer[i]);
	}
	if(buffer!=NULL)
		free(buffer);

	return 0;
}